package com.nanmeishu.springdatajpa.jpademo.repository;

import com.nanmeishu.springdatajpa.jpademo.entity.User;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.repository.query.Param;

import java.util.List;

/*
 *  @RepositoryDefinition(domainClass = User.class,idClass = Integer.class)的效果
 *  与extends Repository<User,Integer>一致
 */
public interface UserRepository extends Repository<User,Integer> {
    User getUserById(int id);

    List<User> getUserByAddress_AddressNameContaining(String str);

    @Query("select u from User u where u.age=(select max(uu.age) from User uu)")
    User getMaxAge();

    @Query("select u from User u where u.username=?1 and u.age=?2")
    User getUserByUA(String username,Integer age);

    @Query("select u from User u where u.username=:username and u.age=:age")
    User getUA(@Param("age") Integer age,@Param("username") String username);

    @Query("select u from User u where u.username like %?1%")
    List<User> listUserLike(String str);

    @Query(value = "select * from t_user",nativeQuery = true)
    List<User> listUser();

    @Modifying
    @Query("update User u set u.username=?2 where u.id=?1")
    void updateUser(Integer id,String username);

    @Modifying
    @Query("delete from User u where u.id=?1")
    void deleteUser(Integer id);

}
